﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Text;

namespace VoteList.Layouts.VoteList
{
    public partial class AddCandidate : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            if (!Page.IsPostBack)
            {
                using (SPWeb mWeb = SPContext.Current.Site.RootWeb)
                {
                    SPList mList = mWeb.Lists[Define.DATALISTNAME];
                    SPListItem mItem = GetVoteListItem(mList, mWeb.CurrentUser);
                    if (mItem == null)
                    {
                        TextBox3.Text = SPContext.Current.Web.CurrentUser.LoginName;

                        string userName = SPContext.Current.Web.CurrentUser.Name;
                        int index = userName.IndexOf("(");
                        if (index > -1)
                        {
                            userName = userName.Substring(0, index);
                        }

                        TextBox4.Text = userName;
                    }
                    else
                    {
                        TextBox3.Text = mWeb.CurrentUser.LoginName;
                        TextBox4.Text = mItem[Define.COLUMN_TITLE] != null ? mItem[Define.COLUMN_TITLE].ToString() : string.Empty;
                        TextBox1.Text = mItem[Define.DATACOLUMN_LOCATION] != null ? mItem[Define.DATACOLUMN_LOCATION].ToString() : "请参考工位图填写所在工位编号";
                        TextBox2.Text = mItem[Define.DATACOLUMN_DESIGNDES] != null ? mItem[Define.DATACOLUMN_DESIGNDES].ToString() : string.Empty;
                    }
                }
            }
            #endregion
        }

        public static string Message = string.Empty;

        public static void ShowDialog(Page page, string message)
        {
            try
            {
                Message = message;

                string csname = "ShowDialogScript";
                Type cstype = page.ClientScript.GetType();
                ClientScriptManager cs = page.ClientScript;
                if (!cs.IsStartupScriptRegistered(cstype, csname))
                {
                    StringBuilder js = new StringBuilder();
                    js.Append("<script type='text/javascript'>");
                    js.Append("ExecuteOrDelayUntilScriptLoaded(openDialog, 'sp.ui.dialog.js');");
                    js.Append("</script>");
                    cs.RegisterStartupScript(cstype, csname, js.ToString());
                }
            }
            catch { }
        }

        public SPListItem GetVoteListItem(SPList list, SPUser user)
        {
            SPQuery query = new SPQuery();

            query.Query = "<Where><Eq><FieldRef Name=\"Account\" LookupId=\"TRUE\" /><Value Type=\"User\">" + user.ID + "</Value></Eq></Where>";

            SPListItemCollection itemCollection = list.GetItems(query);

            if (itemCollection.Count > 0)
            {
                return itemCollection[0];
            }
            else
            {
                return null;
            }
        }

        public SPListItem AddVoteListItem(bool isCandidate)
        {
            SPListItem item = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList list = web.Lists[Define.DATALISTNAME];
                        if (list != null)
                        {
                            item = list.AddItem();

                            item[Define.COLUMN_TITLE] = TextBox4.Text.Trim();
                            item[Define.DATACOLUMN_ACCOUNT] = TextBox3.Text;
                            item[Define.COLUMN_ISCANDIDATE] = isCandidate;
                            //item["DesignDes"] = "test";

                            item.Update();
                            list.Update();
                        }
                    }
                }
            });

            return item;
        }

        protected void AddCandidateToList(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (String.IsNullOrEmpty(TextBox1.Text.Trim()))
                {
                    ShowDialog(Page, "请填写您的姓名");
                    return;
                }

                if (TextBox1.Text.Trim().Length > 6)
                {
                    ShowDialog(Page, "您的座位描述应该在6字之内");
                    return;
                }

                if (TextBox2.Text.Trim().Length > 200)
                {
                    ShowDialog(Page, "您的创意描述应该在200字之内");
                    return;
                }

                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList list = web.Lists[Define.DATALISTNAME];

                        SPListItem item = GetVoteListItem(list, SPContext.Current.Web.CurrentUser);

                        if (item == null)
                        {
                            item = AddVoteListItem(true);
                        }
                        else
                        {
                            item[Define.COLUMN_ISCANDIDATE] = true;
                        }

                        item[Define.DATACOLUMN_LOCATION] = TextBox1.Text.Trim();
                        item[Define.DATACOLUMN_DESIGNDES] = TextBox2.Text.Trim();
                        item[Define.COLUMN_TITLE] = TextBox4.Text.Trim();

                        item.SystemUpdate();

                        ShowDialog(Page, "您填写的内容已经提交");
                    }
                }
            });
        }

    }
}
